package gov.fgn.sijyp.general.util;

import gov.fgn.siijt.exhumaciones.diligencia.DocumentoAdjuntoDto;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

public final class HttpResponseUtil {

	public static void downloadFile(DocumentoAdjuntoDto documentoDto) throws IOException {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();

		ec.responseReset();
		ec.setResponseContentType(documentoDto.getMimeType());
		ec.setResponseContentLength(documentoDto.getData().length);
		ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + documentoDto.getNombreArchivo() + "\"");

		ec.getResponseOutputStream().write(documentoDto.getData());
		ec.getResponseOutputStream().flush();
		ec.getResponseOutputStream().close();
					
		fc.responseComplete();		
	}
}
